package com.toman.toolbar;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.device.ScanManager;
import android.device.scanner.configuration.PropertyID;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.support.annotation.ColorInt;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.toman.Enum.BaseEnum;
import com.toman.Util.IntentUtils;
import com.toman.Util.PermissonUtils;
import com.toman.Util.SPUtils;
import com.example.swipebackactivity.app.SwipeBackActivity;
import com.toman.MyApplication;
import com.toman.R;
import com.githang.statusbar.StatusBarCompat;
import com.kingja.loadsir.core.LoadService;
import com.toman.bean.DialogBean;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import me.leefeng.promptlibrary.PromptDialog;


/**
 * @Description:
 * @Author:邹苏隆
 * @Since:2016/12/16 10:54
 */
public abstract class ToolBarActivity extends SwipeBackActivity {
//    空view适配器
private final static String SCAN_ACTION = ScanManager.ACTION_DECODE;//default action

    //public EmptyWrapper mEmptyWrapper;
 public  PromptDialog promptDialog;

    public ProgressBar progressBar;
    public BaseEnum baseEnum;
    protected LoadService loadService;
    protected ToolBarHelper mToolBarHelper;
    public Toolbar toolbar;
    private TextView tvTitle;
    public Handler mHandler;
    public int mLayoutResID;
    public MyApplication myApplication;
    public String token;
   public String msg;
    protected boolean Landscape = false;
//    万能ID
public String id;
public String p_id;
    public   String companyId;
    public   String mCompanyId;
    public   String departmentId;
    public   String departmentName;
    public int page = 2;
    public Dialog dialog;
    public String status = "0";
  public   List<DialogBean> dialogList = new ArrayList<>();
    public Animation slide_left_to_left,slide_right_to_left,slide_left_to_right,slide_left_to_left_in;

    private Vibrator mVibrator;
    private ScanManager mScanManager;
    private SoundPool soundpool = null;
    private int soundid;
    private String barcodeStr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBarColor(getResources().getColor(R.color.colorPrimary));
        PushAgent.getInstance(this).onAppStart();
    }

    @Override
    public void setContentView(int layoutResID) {

        mToolBarHelper = new ToolBarHelper(this, layoutResID);
//                mToolBarHelper.setToolbarTitle("a");
        toolbar = mToolBarHelper.getToolBar();
        promptDialog = new PromptDialog(this);
        setContentView(mToolBarHelper.getContentView());
        ButterKnife.bind(this);;
        /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(toolbar);
        /*自定义的一些操作*/
        onCreateCustomToolBar(toolbar);
        mLayoutResID = layoutResID;
        myApplication = (MyApplication) getApplication();
        mVibrator= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        token = (String) SPUtils.get(getApplicationContext(),"token","");
         slide_left_to_left = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_left_to_left);
         slide_right_to_left = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_right_to_left);
         slide_left_to_right = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_left_to_right);
         slide_left_to_left_in = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_left_to_left_in);
//        mToolBarHelper.setToolbarTitle("a");
        if (SPUtils.contains(getApplicationContext(),"cpy_id_now")){
            companyId= (String) SPUtils.get(getApplicationContext(),"cpy_id_now","");
        }
        else
        {
                companyId= (String) SPUtils.get(getApplicationContext(),"cpy_id","");

        }
        mCompanyId=(String) SPUtils.get(getApplicationContext(),"cpy_id","");
        departmentId=(String) SPUtils.get(getApplicationContext(),"departmentId","");
        departmentName=(String) SPUtils.get(getApplicationContext(),"departmentName","");
        getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    }
    private void initScan() {
        // TODO Auto-generated method stub
        try {
            mScanManager = new ScanManager();
            mScanManager.openScanner();

            mScanManager.switchOutputMode( 0);
            soundpool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 100); // MODE_RINGTONE
            soundid = soundpool.load("/etc/Scan_new.ogg", 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void onCreateCustomToolBar(Toolbar toolbar) {
        tvTitle = (TextView) findViewById(R.id.tv_toolbar_title);
        toolbar.setContentInsetsRelative(0, 0);
        toolbar.inflateMenu(R.menu.toolbar_save);

    }

    @Override
    protected void onResume() {
        /**
         * 设置为横屏
         */
//        if (Landscape) {
//            if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
//                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//            }
//        } else {
//        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        }
//        }

        super.onResume();
        MobclickAgent.onResume(this);
        initScan();
        resumeScan();

    }

    private void resumeScan() {
        try {
            IntentFilter filter = new IntentFilter();
            int[] idbuf = new int[]{PropertyID.WEDGE_INTENT_ACTION_NAME, PropertyID.WEDGE_INTENT_DATA_STRING_TAG};
            String[] value_buf = mScanManager.getParameterString(idbuf);
            if(value_buf != null && value_buf[0] != null && !value_buf[0].equals("")) {
                filter.addAction(value_buf[0]);
            } else {
                filter.addAction(SCAN_ACTION);
            }

            registerReceiver(mScanReceiver, filter);
        } catch (Exception e) {
//            e.printStackTrace();
        }
    }

    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            soundpool.play(soundid, 1, 1, 0, 0, 1);
            mVibrator.vibrate(100);

            byte[] barcode = intent.getByteArrayExtra(ScanManager.DECODE_DATA_TAG);
            int barcodelen = intent.getIntExtra(ScanManager.BARCODE_LENGTH_TAG, 0);
            byte temp = intent.getByteExtra(ScanManager.BARCODE_TYPE_TAG, (byte) 0);
            android.util.Log.i("debug", "----codetype--" + temp);
            barcodeStr = new String(barcode, 0, barcodelen);
            dealScan(barcodeStr);
        }
    };

    protected  void dealScan(String barcodeStr){

    };

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
//        解绑扫码
        try {
            if(mScanManager != null) {
                mScanManager.stopDecode();
            }
            unregisterReceiver(mScanReceiver);
        } catch (Exception e) {
//            e.printStackTrace();
        }

    }

    public void initToolBarNew(String text, int layoutResID) {
        mToolBarHelper = new ToolBarHelper(this, layoutResID);
        mToolBarHelper.setTvTitle(text);
        super.setTitle("");
        setContentView(mToolBarHelper.getContentView());
        toolbar = mToolBarHelper.getToolBar();
  /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(toolbar);
    }

    public void initToolBarNew(String text, @ColorInt int colorId, int layoutResID) {
        mToolBarHelper = new ToolBarHelper(this, layoutResID);
        mToolBarHelper.setTvTitle(text);
        mToolBarHelper.setToolBarColor(colorId);
        setStatusBarColor(colorId);

        super.setTitle("");
        setContentView(mToolBarHelper.getContentView());
        toolbar = mToolBarHelper.getToolBar();
  /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(toolbar);
        if (colorId==getResources().getColor(R.color.white)){
setArrowBlack();
        }
        ButterKnife.bind(this);
    }

    public void initToolBar(String text, @ColorInt int colorId) {
        mToolBarHelper.setTvTitle(text);
        mToolBarHelper.setToolBarColor(colorId);
        setStatusBarColor(colorId);


        super.setTitle("");
        toolbar = mToolBarHelper.getToolBar();
        if (colorId==getResources().getColor(R.color.white)){
            setArrowBlack();

            mToolBarHelper.setTvColor(getResources().getColor(R.color.black));

//            StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.white), false);

        }
        else {
        }
  /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(toolbar);

    }
    public void initToolBar(String text) {
        mToolBarHelper.setTvTitle(text);
        super.setTitle("");
        toolbar = mToolBarHelper.getToolBar();

  /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(toolbar);

    }


    public void isLandscape() {
        Landscape = true;
    }

    public void setTvTitle(String text) {
        mToolBarHelper.setTvTitle(text);
    }
    public void setArrowBlack(){
        toolbar.setNavigationIcon(R.mipmap.back_black);
    }


}